﻿//-----------------------------------------------------------------------
// <copyright file="MLHtmlHelper.cs" company="MAPILab Ltd">
//     Copyright (c) MAPILab Ltd.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace MAPILab.SharePoint.Explorer.Information
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    /// <summary>
    /// Class for work with help
    /// </summary>
    class MLHtmlHelper
    {
        #region Methods

        [DllImport("hhctrl.ocx", CharSet = CharSet.Auto)]
        public static extern int HtmlHelp(HandleRef hwndCaller, [MarshalAs(UnmanagedType.LPTStr)] string pszFile, int uCommand, int dwData);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetActiveWindow();

        /// <summary>
        /// Shows topic in help
        /// </summary>
        /// <param name="parent">Parent form</param>
        /// <param name="prefix">Prefix</param>
        /// <param name="topicIndex">Topic index</param>
        public static void ShowTopic(object parent, string prefix, int topicIndex)
        {
            CultureInfo CurCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
            Assembly assembly = parent.GetType().Module.Assembly;
            string directory = Path.GetDirectoryName(assembly.Location);

            string curLang = CurCulture.TwoLetterISOLanguageName;
            if (curLang != "en" && curLang != "ru")
            {
                curLang = "en";
            }

            string helpPath = string.Format("{0}\\{1}.{2}.chm", directory, prefix, curLang);

            Control parentControl = null;
            if (parent != null && parent is Control)
                parentControl = (Control)parent;

            HandleRef ref1;
            if (parentControl != null)
                ref1 = new HandleRef(parentControl, parentControl.Handle);
            else
                ref1 = new HandleRef(null, GetActiveWindow());

            HtmlHelp(ref1, helpPath, 0x000F, topicIndex);
        }

        #endregion
    }
}
